package com.zenithmind.coding.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.coding.mapper.ProblemMapper.ProblemStatistics;
import com.zenithmind.coding.mapper.ProblemMapper.TagStatistics;
import com.zenithmind.coding.mapper.ProblemMapper.UserSolveStatistics;
import com.zenithmind.coding.pojo.dto.ProblemCreateDTO;
import com.zenithmind.coding.pojo.dto.StatusUpdateRequest;
import com.zenithmind.coding.pojo.vo.ProblemVO;
import com.zenithmind.coding.service.ProblemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编程问题控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "编程问题", description = "编程问题管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/problems")
@RequiredArgsConstructor
public class ProblemController {

    private final ProblemService problemService;

    @Operation(summary = "创建问题", description = "创建新的编程问题")
    @PostMapping
    public Result<ProblemVO> createProblem(@Valid @RequestBody ProblemCreateDTO createDTO) {
        ProblemVO problem = problemService.createProblem(createDTO);
        return Result.success(problem);
    }

    @Operation(summary = "分页查询问题列表", description = "分页查询编程问题列表")
    @GetMapping
    public Result<PageResult<ProblemVO>> getProblemPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty,
            @Parameter(description = "问题类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "问题状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "标签列表") @RequestParam(required = false) List<String> tags) {
        PageResult<ProblemVO> result = problemService.getProblemPage(current, size, categoryId, 
                                                                    difficulty, type, status, keyword, tags);
        return Result.success(result);
    }

    @Operation(summary = "查询问题详情", description = "根据ID查询编程问题详情")
    @GetMapping("/{id}")
    public Result<ProblemVO> getProblemById(@Parameter(description = "问题ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        ProblemVO problem = problemService.getProblemById(id, userId);
        return Result.success(problem);
    }

    @Operation(summary = "更新问题", description = "更新编程问题信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateProblem(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Valid @RequestBody ProblemCreateDTO updateDTO) {
        Boolean result = problemService.updateProblem(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除问题", description = "删除指定编程问题")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.deleteProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "发布问题", description = "发布指定编程问题")
    @PostMapping("/{id}/publish")
    public Result<Boolean> publishProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.publishProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "下架问题", description = "下架指定编程问题")
    @PostMapping("/{id}/unpublish")
    public Result<Boolean> unpublishProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        Boolean result = problemService.unpublishProblem(id);
        return Result.success(result);
    }

    @Operation(summary = "更新问题状态", description = "更新指定编程问题的状态")
    @PutMapping("/{id}/status")
    public Result<Boolean> updateProblemStatus(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "状态") @RequestBody StatusUpdateRequest request) {
        Boolean result = problemService.updateProblemStatus(id, request.getStatus());
        return Result.success(result);
    }

    @Operation(summary = "审核问题", description = "审核指定编程问题")
    @PostMapping("/{id}/review")
    public Result<Boolean> reviewProblem(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "是否通过") @RequestParam Boolean approved,
            @Parameter(description = "审核意见") @RequestParam(required = false) String comment) {
        Boolean result = problemService.reviewProblem(id, approved, comment);
        return Result.success(result);
    }

    @Operation(summary = "查询推荐问题", description = "查询个性化推荐问题")
    @GetMapping("/recommend")
    public Result<List<ProblemVO>> getRecommendProblems(
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = UserContext.getUserId();
        List<ProblemVO> problems = problemService.getRecommendProblems(userId, difficulty, limit);
        return Result.success(problems);
    }

    @Operation(summary = "查询热门问题", description = "查询热门编程问题")
    @GetMapping("/hot")
    public Result<List<ProblemVO>> getHotProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<ProblemVO> problems = problemService.getHotProblems(limit);
        return Result.success(problems);
    }

    @Operation(summary = "查询相关问题", description = "查询相关编程问题")
    @GetMapping("/{id}/related")
    public Result<List<ProblemVO>> getRelatedProblems(
            @Parameter(description = "问题ID") @PathVariable String id,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<ProblemVO> problems = problemService.getRelatedProblems(id, limit);
        return Result.success(problems);
    }

    @Operation(summary = "根据标签查询问题", description = "根据标签查询编程问题")
    @GetMapping("/tags")
    public Result<List<ProblemVO>> getProblemsByTags(
            @Parameter(description = "标签列表") @RequestParam List<String> tags,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<ProblemVO> problems = problemService.getProblemsByTags(tags, limit);
        return Result.success(problems);
    }

    @Operation(summary = "搜索问题", description = "根据关键词搜索编程问题")
    @GetMapping("/search")
    public Result<List<ProblemVO>> searchProblems(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<ProblemVO> problems = problemService.searchProblems(keyword, limit);
        return Result.success(problems);
    }

    @Operation(summary = "收藏问题", description = "收藏指定编程问题")
    @PostMapping("/{id}/favorite")
    public Result<Boolean> favoriteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        Boolean result = problemService.favoriteProblem(id, userId);
        return Result.success(result);
    }

    @Operation(summary = "取消收藏问题", description = "取消收藏指定编程问题")
    @DeleteMapping("/{id}/favorite")
    public Result<Boolean> unfavoriteProblem(@Parameter(description = "问题ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        Boolean result = problemService.unfavoriteProblem(id, userId);
        return Result.success(result);
    }

    @Operation(summary = "获取收藏的问题", description = "获取用户收藏的编程问题")
    @GetMapping("/favorites")
    public Result<List<ProblemVO>> getUserFavoriteProblems(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<ProblemVO> problems = problemService.getUserFavoriteProblems(userId, limit);
        return Result.success(problems);
    }

    @Operation(summary = "获取每日挑战", description = "获取每日编程挑战问题")
    @GetMapping("/daily-challenges")
    public Result<List<ProblemVO>> getDailyChallenges(
            @Parameter(description = "数量") @RequestParam(defaultValue = "3") Integer count) {
        String userId = UserContext.getUserId();
        List<ProblemVO> problems = problemService.getDailyChallenges(userId, count);
        return Result.success(problems);
    }

    @Operation(summary = "获取问题统计", description = "获取编程问题统计信息")
    @GetMapping("/statistics")
    public Result<ProblemStatistics> getProblemStatistics(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty) {
        ProblemStatistics statistics = problemService.getProblemStatistics(categoryId, difficulty);
        return Result.success(statistics);
    }

    @Operation(summary = "获取热门标签", description = "获取热门问题标签")
    @GetMapping("/tags/hot")
    public Result<List<TagStatistics>> getHotTags(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagStatistics> tags = problemService.getHotTags(limit);
        return Result.success(tags);
    }

    @Operation(summary = "获取用户解题统计", description = "获取用户解题统计信息")
    @GetMapping("/user-statistics")
    public Result<UserSolveStatistics> getUserSolveStatistics() {
        String userId = UserContext.getUserId();
        UserSolveStatistics statistics = problemService.getUserSolveStatistics(userId);
        return Result.success(statistics);
    }

    @Operation(summary = "批量导入问题", description = "批量导入编程问题")
    @PostMapping("/batch/import")
    public Result<Integer> batchImportProblems(@Valid @RequestBody List<ProblemCreateDTO> problems) {
        Integer count = problemService.batchImportProblems(problems);
        return Result.success(count);
    }

    @Operation(summary = "导出问题", description = "导出指定编程问题")
    @PostMapping("/batch/export")
    public Result<List<ProblemVO>> exportProblems(@RequestBody List<String> problemIds) {
        List<ProblemVO> problems = problemService.exportProblems(problemIds);
        return Result.success(problems);
    }
}
